package no.ntnu.fp.handlers;

import java.io.IOException;
import java.net.ConnectException;

import no.ntnu.fp.handler.Handler;
import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.impl.LogoutMessage;
import no.ntnu.fp.messages.impl.LogoutSuccessfulMessage;
import no.ntnu.fp.net.co.Connection;
import no.ntnu.fp.server.MainConnection;

public class LogoutHandler implements Handler{

	@Override
	public void handleMessage(Message message) {
		LogoutMessage logout = (LogoutMessage)message;
		Connection connection = MainConnection.getConnection(logout.getEmployee());
		MainConnection.unregisterEmployee(logout.getEmployee());
		try {
			connection.send(new LogoutSuccessfulMessage().toString());
			
			/*
			 * Should the connection be closed by the server, or by the client?
			 */
//			connection.close();
		} catch (ConnectException e) {
			System.err.println("Logout handler encountered an error.\n" + e);
		} catch (IOException e) {
			System.err.println("Logout handler encountered an error.\n" + e);
		}
	}
}
